#include "fpga_interface.h"
#include "sense_node.h"

// 带宽的可选项有: 5k, 25k, 60k, 200k, 1.28m, 6.4m
const uint32_t bandwidth_code_tab[] = {6400, 5, 25, 60, 200, 1280, 6400};

static void iq_dump_to_file(const char *filename, const void *src, size_t length) {
    const int16_t *data = (int16_t *)src;
    FILE *fp;
    fp = fopen(filename, "w");
    if (fp == NULL)
        perror("Failed to open file");

    for (size_t i = 0; i < length / 2; ++i) {
        fprintf(fp, "%d,", data[i]);
        if ((i + 1) % 2 == 0)
            fprintf(fp, "\n");
    }
    fclose(fp);
}

int main(int argc, char *argv[]) {
    uint32_t cnt = 0;
    uint16_t *fft_ram;
    uint32_t freq_khz = 0, bandwidth_code = 0;
    uint32_t samples;

    fpga_init(0);
    fpga_init(1);

    if (argc != 4) {
        printf("请输入频率，带宽值，样点数！\n");
        return -1;
    };
    freq_khz = strtoll(argv[1], NULL, 0);
    bandwidth_code = strtoll(argv[2], NULL, 0);
    samples = strtoll(argv[3], NULL, 0);

    uint32_t bytes = samples * 4; // 一组IQ长度4个字节

    start_DDC(freq_khz, bandwidth_code, 0);
    start_DDC(freq_khz, bandwidth_code, 1);
    if (bandwidth_code > 6)
        bandwidth_code = 0;
    uint64_t store_time =
        (uint64_t)samples * 1000000000 / 200 / bandwidth_code_tab[bandwidth_code] / 1000;
    printf("存储时长: %lld * 200ns\n", store_time);

    set_timestamp(1, 1, 1, 1);
    store_DDC_result(store_time, bytes / 32, 0);
    store_DDC_result(store_time, bytes / 32, 1);

    uint8_t *data2 = (uint8_t *)malloc(bytes * sizeof(uint8_t));
    if (wait_DDC() == -1) {
        printf("DDC Timeout!\n");
    }
    stop_DDC(0);
    stop_DDC(1);

    printf("开始读DDR\n");

    uint32_t bytes_to_read = bytes;
    uint32_t bytes_already_read = 0;
    while (bytes_to_read > 0) {
        if (bytes_to_read >= 0x100000) {
            read_fpga_ddr(bytes_already_read / 32, 0x100000 / 32, data2 + bytes_already_read, 0);
            bytes_to_read -= 0x100000;
            bytes_already_read += 0x100000;
        } else {
            read_fpga_ddr(bytes_already_read / 32, bytes_to_read / 32, data2 + bytes_already_read,
                          0);
            bytes_to_read = 0;
        }
    }
    printf("count: %d\n", (spi_read_fpga(1, 0x41) << 16) | spi_read_fpga(1, 0x40));
    spi_write_fpga(1, 0x40, 0);
    spi_write_fpga(1, 0x41, 0);
    iq_dump_to_file("/mnt/data1.csv", data2, bytes);

    bytes_to_read = bytes;
    bytes_already_read = 0;
    while (bytes_to_read > 0) {
        if (bytes_to_read >= 0x100000) {
            read_fpga_ddr(bytes_already_read / 32, 0x100000 / 32, data2 + bytes_already_read, 1);
            bytes_to_read -= 0x100000;
            bytes_already_read += 0x100000;
        } else {
            read_fpga_ddr(bytes_already_read / 32, bytes_to_read / 32, data2 + bytes_already_read,
                          1);
            bytes_to_read = 0;
        }
    }
    printf("count: %d\n", (spi_read_fpga(1, 0x43) << 16) | spi_read_fpga(1, 0x42));
    spi_write_fpga(1, 0x42, 0);
    spi_write_fpga(1, 0x43, 0);
    iq_dump_to_file("/mnt/data2.csv", data2, bytes);

    uint8_t day, hour, min, sec;
    uint32_t ns2048;
    read_start_time_ch1(&day, &hour, &min, &sec, &ns2048);
    printf("开始时间：%d 日 %d 时 %d 分 %d 秒\n", day, hour, min, sec);
    printf("%u / 204800000 秒\n", ns2048);

    read_end_time_ch1(&day, &hour, &min, &sec, &ns2048);
    printf("结束时间：%d 日 %d 时 %d 分 %d 秒\n", day, hour, min, sec);  
    printf("%u / 204800000 秒\n", ns2048);

    read_start_time_ch2(&day, &hour, &min, &sec, &ns2048);
    printf("开始时间：%d 日 %d 时 %d 分 %d 秒\n", day, hour, min, sec);
    printf("%u / 204800000 秒\n", ns2048);

    read_end_time_ch2(&day, &hour, &min, &sec, &ns2048);
    printf("结束时间：%d 日 %d 时 %d 分 %d 秒\n", day, hour, min, sec);  
    printf("%u / 204800000 秒\n", ns2048);

    close_fpga(0);
    close_fpga(1);
    return 0;
}
